﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;
using Microsoft.Win32;

namespace ProStore
{
    class Registro_control
    {
        private string passphrasestring = "prostore1.0";

        RegistryKey regKeyAppRoot = Registry.CurrentUser.OpenSubKey("Software", true);

        public string[] ReadServerInfo()
        {
            Check_Registry();

            string[] data = new string[4];

            try
            {
                data[0] = ReadReg("ServerIP");
                data[1] = ReadReg("User");

                string password = ReadReg("Password");
                if(password != "")data[2] = StringCipher.Decrypt(password, passphrasestring);
                return data;
            }catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return null;
            }
        }

        public bool SaveServerInfo(string serverip, string user, string password)
        {
            string encpassword = StringCipher.Encrypt(password, passphrasestring);

            try
            {
                WriteReg("ServerIP", serverip);
                WriteReg("User", user);
                WriteReg("Password", encpassword);
                return true;
            }catch(Exception e)
            {
                throw new System.ArgumentException(e.Message, "SaveServerInfo", e);
            }
        }

        private void Check_Registry()
        {
            RegistryKey regKeyApp = regKeyAppRoot.OpenSubKey("ProStore", true);

            if (regKeyApp == null)
            {
                try
                {
                    regKeyAppRoot.CreateSubKey("ProStore");
                    RegistryKey valueApp = regKeyAppRoot.OpenSubKey("ProStore", true);
                    valueApp.SetValue("ServerIP", "");
                    valueApp.SetValue("User", "");
                    valueApp.SetValue("Password", "");
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            }
        }
        private string ReadReg(string KeyName)
        {
            string strWindowsState;
            RegistryKey regKeyApp = regKeyAppRoot.OpenSubKey("ProStore", true);

            try
            {
                strWindowsState = (string)regKeyApp.GetValue(KeyName);
                return strWindowsState;
            }
            catch (Exception e)
            {
                throw new System.ArgumentException(e.Message, "ReadReg", e);
            }
        }

        private void WriteReg(string KeyName, object Value)
        {
            RegistryKey regKeyApp = regKeyAppRoot.OpenSubKey("ProStore", true);

            try
            {
                regKeyApp.SetValue(KeyName, Value);
            }
            catch (Exception e)
            {
                throw new System.ArgumentException(e.Message, "ReadReg", e);
            }
        }
    }
    public static class StringCipher
    {
        private const string initVector = "tu89geji340t89u2";

        private const int keysize = 256;

        public static string Encrypt(string plainText, string passPhrase)
        {
            byte[] initVectorBytes = Encoding.UTF8.GetBytes(initVector);
            byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
            PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null);
            byte[] keyBytes = password.GetBytes(keysize / 8);
            RijndaelManaged symmetricKey = new RijndaelManaged();
            symmetricKey.Mode = CipherMode.CBC;
            ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
            cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
            cryptoStream.FlushFinalBlock();
            byte[] cipherTextBytes = memoryStream.ToArray();
            memoryStream.Close();
            cryptoStream.Close();
            return Convert.ToBase64String(cipherTextBytes);
        }

        public static string Decrypt(string cipherText, string passPhrase)
        {
            byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
            byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
            PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null);
            byte[] keyBytes = password.GetBytes(keysize / 8);
            RijndaelManaged symmetricKey = new RijndaelManaged();
            symmetricKey.Mode = CipherMode.CBC;
            ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
            MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
            CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
            byte[] plainTextBytes = new byte[cipherTextBytes.Length];
            int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
            memoryStream.Close();
            cryptoStream.Close();
            return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
        }
    }
}
